import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入token
import store from '@/store/index'

// 一级路由
import Login from '@/views/login'
import Layout from '@/views/layout'
import Search from '@/views/search'
import Detail from '@/views/Detail'

// 二级路由
import Homepage from '@/views/homepage'
import Classification from '@/views/classification'
import Shopping from '@/views/shopping'
import My from '@/views/my'
Vue.use(VueRouter)

// const routes = [

// ]

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    { path: '/search', component: Search },
    { path: '/detail', component: Detail },
    {
      path: '/',
      component: Layout,
      redirect: '/homepage',
      children: [
        { path: 'homepage', component: Homepage },
        { path: 'classification', component: Classification },
        { path: 'shopping', component: Shopping },
        { path: 'my', component: My }
      ]
    }
  ]
})

const whiet = ['/login']
router.beforeEach((to, from, next) => {
  next()
  const token = store.state.user.token
  console.log(token)
  if (token) {
    next()
    return
  }
  if (whiet.includes(to.path)) {
    next()
    return
  }
  next('/login')
})
export default router
